package uni.call.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.*;

public class IncomingCallReceiver extends BroadcastReceiver
{
	public static SipAudioCall IncomingCall = null;
	
    @Override
    public void onReceive(final Context context, Intent intent) 
    {   
        SipAudioCall.Listener listener = new SipAudioCall.Listener()
        {
        	@Override
            public void onCallEnded(SipAudioCall call)
            {
    			IncomingCallAnswerActivity.RingTone.stop();
    			try 
    			{
					call.endCall();
				} 
    			catch (SipException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    			Intent MyIncomingCallActivity = new Intent(context,HomeActivity.class); 
    			context.startActivity(MyIncomingCallActivity);
            }
        };
        
        try 
        {
        	IncomingCall = HomeActivity.SipManager.takeAudioCall(intent, listener);
        	IncomingCall.setListener(listener);
        } 
        catch (SipException e) 
        {
			e.printStackTrace();
		}
        
	    Intent MyIncomingCallActivity = new Intent(context,IncomingCallAnswerActivity.class); 
	    MyIncomingCallActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    MyIncomingCallActivity.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
	    MyIncomingCallActivity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	    MyIncomingCallActivity.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
	    context.startActivity(MyIncomingCallActivity);
    } 
}
